package co.fitstart.scheduler.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.scheduler.service.CourseScheduleService;

@Component
public class CourseScheduleJob {
    
    private static final Logger logger = LoggerFactory.getLogger(CourseScheduleJob.class);
    
    @Autowired
    private CourseScheduleService courseScheduleService;
    
    /**
     * 
     * 
     * run at 2:00 am every day
     */
    @Scheduled(cron = "0 40 2 * * *")
    public void scanCourseScheduleExpiry() {
        
        Date current = new Date();
        Date now = DateUtils.parseDate(DateFormatUtils.format(current, "yyyy-MM-dd"), "yyyy-MM-dd");
        
        logger.info("start running CourseScheduleJob#scanCourseScheduleExpiry at {} and compare date = {}", current, now);
        
        // scan order pay timeout
        int toExpireCourseSchedule = courseScheduleService.toExpiryCourseSchedule(now);
        logger.info("scaned {} courseSchedule need to be expired before expire course schedule", toExpireCourseSchedule);
        
        if(toExpireCourseSchedule > 0) {
            courseScheduleService.expiryCourseSchedule(now);
            
            toExpireCourseSchedule = courseScheduleService.toExpiryCourseSchedule(now);
            logger.info("scaned {} courseSchedule need to be expired after expire courseSchedule", toExpireCourseSchedule);
        }
        
        logger.info("finish running CourseScheduleJob#scanCourseScheduleExpiry at {}", new Date());
        
    }
    
    //@Scheduled(cron = "0 30 * * * *")
    public void scanTodayCourseScheduleExpire() {
    	
    	Date current = new Date();
        String now = DateFormatUtils.format(current, "yyyy-MM-dd");
        courseScheduleService.addExpire(now);
             
    }
    
}
